
package com.sylar.happyreader.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.ViewConfiguration;
import android.widget.GridView;

import com.sylar.happyreader.R;

public class ShelvesView extends GridView {
    private Bitmap mShelfBackground;
	private Bitmap backgroundLeft;
	private Bitmap backgroundRight;
    private int mShelfWidth;
    private int mShelfHeight;

    private Bitmap mWebLeft;
    private Bitmap mWebRight;
    private int mWebRightWidth;

    public ShelvesView(Context context) {
        super(context);
        init(context);
    }

    public ShelvesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        load(context, attrs, 0);
		init(context);
    }

    public ShelvesView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        load(context, attrs, defStyle);
        init(context);
    }
    
	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {

		int width = bitmap.getWidth();

		int height = bitmap.getHeight();

		Matrix matrix = new Matrix();

		float scaleWidht = ((float) w / width);

		float scaleHeight = ((float) h / height);

		matrix.postScale(scaleWidht, scaleHeight);

		Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,

		true);

		return newbmp;

	}

    private void load(Context context, AttributeSet attrs, int defStyle) {

        final Resources resources = getResources();
        backgroundLeft = BitmapFactory.decodeResource(resources,R.drawable.bookshelf_layer_left);
		backgroundRight = BitmapFactory.decodeResource(resources,R.drawable.bookshelf_layer_right);
		
		int bLeftWidth = backgroundLeft.getWidth();
		int width = getResources().getDisplayMetrics().widthPixels;
        Bitmap shelfBackground = BitmapFactory.decodeResource(resources,R.drawable.bookshelf_layer_center);
        if (shelfBackground != null) {
        	
            mShelfWidth = shelfBackground.getWidth();
            mShelfHeight = shelfBackground.getHeight();
            mShelfBackground = zoomBitmap(shelfBackground, width -bLeftWidth  , mShelfHeight);
            
        }
       
        mWebLeft = BitmapFactory.decodeResource(resources, R.drawable.web_left);
        mWebRight = BitmapFactory.decodeResource(resources, R.drawable.web_right);
        mWebRightWidth = mWebRight.getWidth();

    }

    private void init(Context context) {
        StateListDrawable drawable = new StateListDrawable();

        SpotlightDrawable start = new SpotlightDrawable(context, this);
        start.disableOffset();
        SpotlightDrawable end = new SpotlightDrawable(context, this, R.drawable.spotlight_blue);
        end.disableOffset();
        TransitionDrawable transition = new TransitionDrawable(start, end);
        drawable.addState(new int[] { android.R.attr.state_pressed },
                transition);

        final SpotlightDrawable normal = new SpotlightDrawable(context, this);
        drawable.addState(new int[] { }, normal);

        normal.setParent(drawable);
        transition.setParent(drawable);

        setSelector(drawable);
        setDrawSelectorOnTop(false);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        final int count = getChildCount();
        final int top = count > 0 ? getChildAt(0).getTop() : 0;
        mShelfWidth = mShelfBackground.getWidth();
        mShelfHeight = mShelfBackground.getHeight();
        final int width = getWidth();
        final int height = getHeight();
//        final Bitmap background = mShelfBackground;
        int rwidth = backgroundRight.getWidth();
        
        for (int y = top; y < height; y += mShelfHeight) {
			for (int x = 0; x < width; x += mShelfWidth) {
				canvas.drawBitmap(mShelfBackground, x, y, null);
			}
			canvas.drawBitmap(backgroundLeft, 0, y, null);
			canvas.drawBitmap(backgroundRight, width - rwidth, y, null);
		}
        
        if (count == 0) {
            canvas.drawBitmap(mWebLeft, rwidth, top, null);
            canvas.drawBitmap(mWebRight, width - mWebRightWidth, top + mShelfHeight, null);
        }

        super.dispatchDraw(canvas);
    }

    

    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);

        final Drawable current = getSelector().getCurrent();
        if (current instanceof TransitionDrawable) {
            if (pressed) {
                ((TransitionDrawable) current).startTransition(
                        ViewConfiguration.getLongPressTimeout());
            } else {
                ((TransitionDrawable) current).resetTransition();
            }
        }
    }
}
